package cn.csc.module.member.dal.mysql.role;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.member.controller.app.role.vo.AppMemberRoleExportReqVO;
import cn.csc.module.member.controller.app.role.vo.AppMemberRolePageReqVO;
import cn.csc.module.member.dal.dataobject.role.MemberRoleDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigInteger;
import java.util.List;

/**
 * 会员角色 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface MemberRoleMapper extends BaseMapperX<MemberRoleDO> {

    default PageResult<MemberRoleDO> selectPage(AppMemberRolePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MemberRoleDO>()
                .likeIfPresent(MemberRoleDO::getName, reqVO.getName())
                .eqIfPresent(MemberRoleDO::getCode, reqVO.getCode())
                .eqIfPresent(MemberRoleDO::getSort, reqVO.getSort())
                .eqIfPresent(MemberRoleDO::getType, reqVO.getType())
                .eqIfPresent(MemberRoleDO::getStatus, reqVO.getStatus())
                .eqIfPresent(MemberRoleDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MemberRoleDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MemberRoleDO::getId));
    }

    default List<MemberRoleDO> selectList(AppMemberRoleExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<MemberRoleDO>()
                .likeIfPresent(MemberRoleDO::getName, reqVO.getName())
                .eqIfPresent(MemberRoleDO::getCode, reqVO.getCode())
                .eqIfPresent(MemberRoleDO::getSort, reqVO.getSort())
                .eqIfPresent(MemberRoleDO::getType, reqVO.getType())
                .eqIfPresent(MemberRoleDO::getStatus, reqVO.getStatus())
                .eqIfPresent(MemberRoleDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MemberRoleDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MemberRoleDO::getId));
    }

    @Select("SELECT mr.id, `name`, `code`, sort, `type`, `status`, remark\n" +
            "FROM member_role mr\n" +
            "INNER JOIN member_user_role mur\n" +
            "ON mr.id = mur.role_id\n" +
            "WHERE mur.user_id = #{userId}")
    List<MemberRoleDO> selectByUserId(@Param("userId")BigInteger userId);


    default MemberRoleDO selectByCode(String code){
        return selectOne(new LambdaQueryWrapperX<MemberRoleDO>()
                .eq(MemberRoleDO::getCode, code)
                .last("limit 1")
        );
    }

}
